package com.probiz.estore.system.dao.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import com.probiz.estore.common.model.system.Region;
import com.probiz.estore.common.model.system.RegionItem;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.system.dao.RegionItemDao;


/**
 * RegionItem Data Access Object (DAO) implementation.
 * Developer introduced interfaces should be declared here. Won't get overwritten.
*/
public class RegionItemDaoImpl extends HibernateGenericDaoImpl<RegionItem> implements RegionItemDao {
	
	public void saveRegionItemsFromArrays(Region entity, String[] ids, String[] itemIds){
		//删除子区域值
		List<String> regionItemIdList= Arrays.asList(ids);
		Set<RegionItem> formerRegionItemList=entity.getRegionItems();//原来的子区域值
		for (RegionItem regionItem : formerRegionItemList) {
			if(!regionItemIdList.contains(regionItem.getRegionItemId().toString())){
				delete(regionItem);
			}
		}
		//增加或者修改子区域值
		for (int i = 0; i < ids.length; i++) {
			RegionItem regionItem;
			if(ids[i].equals("")){
				//case:新增
				regionItem = new RegionItem();
			}else{
				//case:修改
				regionItem = this.getById(Integer.valueOf(ids[i]));
			}
			regionItem.setItemRegionId(Integer.valueOf(itemIds[i]));
			regionItem.setRegionId(entity.getRegionId());
			save(regionItem);
		}
	}
}